package nollab.nolapp.app.toto.match;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SelectedMatchDB extends SQLiteOpenHelper {

    public SQLiteDatabase mDb;
	
	public SelectedMatchDB(Context context, String name) {
		super(context, name, null, 0);
		// TODO Auto-generated constructor stub
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("CREATE TABLE sel_matchDB(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ "fb_id TEXT, home TEXT, away TEXT, win_rate INTEGER, created_time INTEGER , bet TEXT);");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE");
		onCreate(db);
	}
	
	public boolean insert(int fb_id, String home, String away, String created_time,String bet)
	{
		ContentValues cv = new ContentValues();
		
		cv.put("fb_id", fb_id);
		cv.put("home", home);
		cv.put("away", away);
		cv.put("created_time", created_time);
		cv.put("bet", bet);
		
		return mDb.insert("sel_matchDB", null, cv) != -1;
	}

	public ArrayList<SelectedMatch> getAllSelectedMatch()
	{
		ArrayList<SelectedMatch> match = new ArrayList<SelectedMatch>();
		  Cursor c = mDb.query("sel_matchDB",
				  new String[] {"fb_id", "home", "away","created_time","bet"}, 
				  null, null, null, null, "id" + " ASC");

	        if (c.moveToFirst()) {
	            final int indexId = c.getColumnIndex("fb_id");
	            final int indexHome = c.getColumnIndex("home");
	            final int indexAway = c.getColumnIndex("away");
	            final int indexCreated_time = c.getColumnIndex("created_time");
	            final int indexBet = c.getColumnIndex("bet");

	            do {
	                int id = c.getInt(indexId);
	                String home = c.getString(indexHome);
	                String away = c.getString(indexAway);
	                String created_time = c.getString(indexCreated_time);
	                String bet = c.getString(indexBet);
	                
	                match.add(new SelectedMatch(id,home,away,created_time,bet));
	                
	            } while (c.moveToNext());
	        }
	        c.close();

	        return match;
	}
	
	
	
}